<template>
  <div>
    <el-form ref="roleForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" style="width:50%" placeholder="请输入内容" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" style="width:50%" placeholder="请输入内容" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="form.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" style="width:50%" placeholder="请输入内容" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="form.departmentName"
          style="width:50%"
          placeholder="请输入内容"
          @focus="hFocus"
        />
        <div class="tree-box">
          <!-- :props="{ label: 'name' }"   设置以哪个字段为准来显示节点-->
          <el-tree
            v-show="isTree"
            :data="treeData"
            :props="{ label: 'name' }"
            :default-expand-all="true"
            @node-click="nodeClick"
          />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="form.correctionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item>
      <el-form-item>
        <el-button @click="hClose">取消</el-button>
        <el-button type="primary" @click="hSubmit">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addStaff } from '@/api/employees'
// 获取常量 正式与非正式数据
import employees from '@/constant/employees'
// 获取部门树形数据
import { getDepartments } from '@/api/departments'
// 引入树形结构转换方法
import { toTree } from '@/utils/index'
export default {
  name: '',
  components: {},
  props: {},
  data() {
    return {
      hireType: employees.hireType,
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      },
      form: {
        username: '',
        mobile: '',
        workNumber: '',
        formOfEmployment: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      treeData: [], // 定义数组接受树形数据
      isTree: false
    }
  },
  computed: {},
  watch: {},
  created() {
  },
  mounted() {},
  methods: {
    //   表单取消
    hClose() {
      this.$emit('close')
    },
    // 添加数据
    async doAdd() {
      try {
        const res = await addStaff(this.form)
        console.log(res)
        this.$message.success('添加成功')
        this.$emit('update-empolyee')
      } catch (err) {
        console.log(err)
        this.$message.error('添加失败')
      }
    },
    // 确定兜底 发送数据
    hSubmit() {
      this.$refs.roleForm.validate(valid => {
        if (valid) {
          // 发送添加数据
          this.doAdd()
          // 通知父组件渲染数据
          this.$emit('update-empolyee')
        } else {
          this.$message.error('添加失败 请重新确让格式是否正确')
          return false
        }
      })
    },
    // 部门数据渲染
    async loadDepartments() {
      try {
        const res = await getDepartments()
        res.data.depts.shift()
        this.treeData = toTree(res.data.depts)
      } catch (err) {
        console.log('', err)
      }
    },
    // 部门获取焦点时触发
    hFocus() {
      // 1.获取数据
      this.loadDepartments()
      // 2.显示树形图
      this.isTree = true
    },
    // 数节点被点击触发
    nodeClick(data) {
      // 有子部门则不能选中
      if (data.children.length) {
        return
      }
      // 1.填充数据
      this.form.departmentName = data.name
      // 2. 隐藏树形图
      this.isTree = false
    }
  }
}
</script>

<style scoped lang='less'>

</style>
